//
//  JKProgressCircle.m
//  OCTest
//
//  Created by 王冲 on 2018/11/20.
//  Copyright © 2018年 希爱欧科技有限公司. All rights reserved.
//

#import "JKProgressCircle.h"
#import "JKGCDTimer.h"
@interface JKProgressCircle()

@property(nonatomic,strong) NSString *gcdTimerKeyName;

/** 圆环的进度变化 */
@property (nonatomic, assign) CGFloat progressCircleValue;
/** 显示的百分比的Label */
@property (nonatomic, weak) UILabel *cLabel;
/** 定时器打印辅助值 */
@property (nonatomic, assign) float value;

@end

@implementation JKProgressCircle

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        
        //默认进度圆环背景颜色
        self.progerssBackgroundColor=[UIColor lightGrayColor];
        //默认进度圆环颜色
        self.progerssColor=[UIColor blueColor];
        //默认进度条宽度
        self.progerWidth=15;
        //默认百分比字体大小
        self.percentageFontSize=22;
        self.percenTageFontColor=[UIColor blueColor];
        // 默认不隐藏
        self.isPercenTageHiden = NO;
        // 进度条的方向，默认逆时针
        self.progerssDirection = NO;
        // 默认开启定时器
        self.isDCGTimerOpen = YES;
        
        //百分比标签
        UILabel *cLabel = [[UILabel alloc] initWithFrame:self.bounds];
        cLabel.font = [UIFont boldSystemFontOfSize:self.percentageFontSize];
        cLabel.textColor = self.percenTageFontColor;
        cLabel.textAlignment = NSTextAlignmentCenter;
        cLabel.hidden = self.isPercenTageHiden;
        [self addSubview:cLabel];
        self.cLabel = cLabel;
    }
    
    return self;
}

-(void)setProgressValue:(CGFloat)progressValue{
    
    _progressValue = progressValue;
    
    if (_isDCGTimerOpen && (_progressValue != 0)) {
        
        __weak typeof(self) weakSelf = self;
        
        self.value = 0;
        
        self.gcdTimerKeyName = [JKGCDTimer execTask:^{
            
            NSLog(@"----------定时器打印-----------");
            
            dispatch_sync(dispatch_get_main_queue(), ^{
                
                NSLog(@"value==%lf jkVlue==%f",weakSelf.value,weakSelf.progressValue);
                
                if (weakSelf.value > weakSelf.progressValue) {
                    
                    [JKGCDTimer canleGCDTimerTask:weakSelf.gcdTimerKeyName];
                    weakSelf.value = 0;
                    NSLog(@"进度完成");
                    return;
                }
                
                weakSelf.value += 0.001;
                weakSelf.progressCircleValue = weakSelf.value;
                weakSelf.cLabel.text = [NSString stringWithFormat:@"%d%%", (int)floor(weakSelf.value * 100)];
            });
            
        } startTime:0 intervalTime:0.001f repeats:YES async:YES];
    }else{
        
        self.progressCircleValue = _progressValue;
        self.cLabel.text = [NSString stringWithFormat:@"%d%%", (int)floor(_progressValue * 100)];
    }
}

#pragma mark 圆环进度的变化
-(void)setProgressCircleValue:(CGFloat)progressCircleValue{
    
    _progressCircleValue = progressCircleValue;
    [self setNeedsDisplay];
}


-(void)setIsDCGTimerOpen:(BOOL)isDCGTimerOpen{
    
    _isDCGTimerOpen = isDCGTimerOpen;
}

-(void)setPercentageFontSize:(CGFloat)percentageFontSize{
    
    _percentageFontSize = percentageFontSize;
    self.cLabel.font = [UIFont boldSystemFontOfSize:_percentageFontSize];
}

-(void)setPercenTageFontColor:(UIColor *)percenTageFontColor{
    
    _percenTageFontColor = percenTageFontColor;
    self.cLabel.textColor = _percenTageFontColor;
}

-(void)setProgerssDirection:(BOOL)progerssDirection{
    
    _progerssDirection = progerssDirection;
}

-(void)setIsPercenTageHiden:(BOOL)isPercenTageHiden{
    
    _isPercenTageHiden = isPercenTageHiden;
    self.cLabel.hidden = _isPercenTageHiden;
}

#pragma mark 重绘界面
- (void)drawRect:(CGRect)rect
{
    //路径
    UIBezierPath *backgroundPath = [[UIBezierPath alloc] init];
    //线宽
    backgroundPath.lineWidth = self.progerWidth;
    //颜色
    [self.progerssBackgroundColor set];
    //拐角
    backgroundPath.lineCapStyle = kCGLineCapRound;
    backgroundPath.lineJoinStyle = kCGLineJoinRound;
    
    //半径
    CGFloat radius = (MIN(rect.size.width, rect.size.height) - self.progerWidth) * 0.5;
    //画弧（参数：中心、半径、起始角度(3点钟方向为0)、结束角度、是否顺时针）
    [backgroundPath addArcWithCenter:(CGPoint){rect.size.width * 0.5, rect.size.height * 0.5} radius:radius startAngle:M_PI * 1.5 endAngle:M_PI * 1.5 + (self.progerssDirection ? 1 : -1)*M_PI * 2  clockwise:self.progerssDirection];
    //连线
    [backgroundPath stroke];
    
    //路径
    UIBezierPath *progressPath = [[UIBezierPath alloc] init];
    //线宽
    progressPath.lineWidth = self.progerWidth;
    //颜色
    [self.progerssColor set];
    //拐角
    progressPath.lineCapStyle = kCGLineCapRound;
    progressPath.lineJoinStyle = kCGLineJoinRound;
    
    //画弧（参数：中心、半径、起始角度(3点钟方向为0)、结束角度、是否顺时针）
    [progressPath addArcWithCenter:(CGPoint){rect.size.width * 0.5, rect.size.height * 0.5} radius:radius startAngle:M_PI * 1.5 endAngle:M_PI * 1.5 + (self.progerssDirection ? 1 : -1)* M_PI * 2 * _progressCircleValue clockwise:self.progerssDirection];
    //连线
    [progressPath stroke];
}

-(void)dealloc{
    
    NSLog(@"gcdTimerKeyName=========%@",self.gcdTimerKeyName);
    
    [JKGCDTimer canleGCDTimerTask:self.gcdTimerKeyName];
}

@end
